#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//从键盘读数据，写入管道，读取管道，写入屏幕
//匿名管道
int main()
{
    int fds[2];
    char buf[100];
    int len;
    //int fd0 = open("pipefd0", O_WRONLY|O_CREAT,0644);
    //int fd1 = open("pipefd1", O_WRONLY|O_CREAT,0644);
    //fds[0]=fd0;
    //fds[1]=fd1;
    if(pipe(fds)==-1)
    {
        perror("make pipe!");
        exit(1);
    }
    while((fgets(buf, 100, stdin))){
        int len = strlen(buf);
        //write pipe
        if(write(fds[1], buf, len)!=len){
            perror("write");
            break;
        }
        memset(buf, 0x00, sizeof(buf));
        if((len=read(fds[0], buf, 100))==-1){
            perror("read!");
            break;
        }
        if(write(1, buf, len)!=len){
            perror("stdout");
            break;
        }
    }
    return 0;
}